<?php
/***************************************************************************
 *
 *   copyright            : (C) 2011 Winds of Storm
 *
 *   $Id: warcommands.php 131 2011-06-12 20:24:56Z stormerider $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

$war_commands	= array('TYPE'		=>	'command',
			'NAME'		=>	'Word War Commands',
			'VERSION'	=>	'0.0.1',
			'CREDIT'	=>	'StormeRider',
			'FUNC'		=>	'pluginWarCommands',
			'HELP'		=>	'pluginWarCommandsHelp');
$plugins[]	= $war_commands;

function pluginWarCommandsHelp($help) {
	global	$war_commands;

	$help['warcount']	= array("/warcount [number]",
					"Set your current word count to [number]" .
					pluginCredit($war_commands));
	$help['warcurrent']	= array("/warcurrent [number]",
					"Set your current word count to [number]" .
					pluginCredit($war_commands));
	$help['waradd']		= array("/waradd [number]",
					"Add [number] to your current word count." .
					pluginCredit($war_commands));
	$help['warsub']		= array("/warsub [number]",
					"Subtract [number] from your current word count." .
					pluginCredit($war_commands));
	$help['warend']		= array("/warend",
					"End your Word War; report word counts and clear them." .
					pluginCredit($war_commands));
	$help['warget']		= array("/warget",
					"Report your Word War stats to the room." .
					pluginCredit($war_commands));
	$help['wargoal']	= array("/wargoal [number]",
					"Change your Word War goal to [number]." .
					pluginCredit($war_commands));
	$help['warset']		= array("/warset [number]",
					"Set your current word count to [number]" .
					pluginCredit($war_commands));
	$help['warstart']	= array("/warstart [number1] [number2]",
					"Start a Word War; set starting word count to [number1] and goal to [number2]." .
					pluginCredit($war_commands));
	$help['warstats']	= array("/warstats",
					"Show a list of the current stats of everyone participating in a Word War." .
					pluginCredit($war_commands));
	$help['wartimer1']	= array("/wartimer",
					"Set a 1 hour timer for the Word War." .
					pluginCredit($war_commands));
	$help['wartimer2']	= array("/wartimer [number]",
					"Set a [number] minute timer for the Word War." .
					pluginCredit($war_commands));
	$help['wartimer3']	= array("/wartimer [number] [message]",
					"Set a [number] minute timer for the Word War. [message] will be sent when the war has been completed." .
					pluginCredit($war_commands));
	$help['warextend1']	= array("/warextend [number] [mine]",
					"Extend all current Word War timers started by the user by [number] minutes." .
					pluginCredit($war_commands));
	$help['warextend2']	= array("/warextend [number] [all]",
					"Extend all current Word War timers by [number] minutes. (Moderator only)" .
					pluginCredit($war_commands));

	return $help;
}

function pluginWarCommands($userinfo, $event) {
	global	$config;
	global	$db;
	global	$dbprefix;
	global	$p_row;

	$userid		= $userinfo['userid'];
	$username	= $userinfo['username'];
        $isMod          = $userinfo['isMod'];

	$msg	= $event[0]['msg'];
	$room	= $event[0]['room'];

	if ((eregi("^/warcurrent", $msg)) || (eregi("^/warset", $msg)) || (eregi("^/warcount", $msg))) {
		$event[0]['parsed']	= 1;
		$warstats	= explode(" ", $msg);
		$cur		= $warstats[1];
		if (is_numeric($cur)) {
			$oldcur	= $p_row['user_word_current'];
			$sql	= "UPDATE {$dbprefix}prefs SET user_word_current=$cur WHERE user_id={$userid}";
			$db->sql_query($sql);

			$delta	= $cur - $oldcur;
			if ($delta > 0) {
				$dstr	= "(+$delta)";
			} else if ($delta < 0) {
				$dstr	= "($delta)";
			} else {
				$dstr	= "";
			}
			$msg	= "<span class=sysmsg><i>*** $username current word count: <b>$cur $dstr</b></i>";
			if ($cur >= $p_row['user_word_goal']) {
				$msg .= "<br /><font color=red><i>*** $username has reached their goal!!</i></font>";
			}
			$msg	.= "</span>";
			$event[0]['msg']	= addslashes($msg);
		} else {
			$event[0]['room']	= 0;
			$event[0]['userto']	= $userid;
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** ERROR: '$cur' is not a valid number to set your current word count to.</b></span>");
		}
	} else if (eregi("^/wargoal", $msg)) {
		$event[0]['parsed']	= 1;
		$warstats	= explode(" ", $msg);
		$goal		= $warstats[1];
		if (is_numeric($goal)) {
			$sql	= "UPDATE {$dbprefix}prefs SET user_word_goal=$goal WHERE user_id={$userid}";
			$db->sql_query($sql);
			$p_row['user_word_goal']	= $goal;

			$msg	= "<span class=sysmsg><i>*** $username changes their word count goal to: <b>{$p_row['user_word_goal']}</b></i>";
			if ($p_row['user_word_current'] >= $p_row['user_word_goal']) {
				$msg .= "<br /><font color=red><i>*** $username has reached their goal!!</i></font>";
			}
			$msg	.= "</span>";
			$event[0]['msg']	= addslashes($msg);
		} else {
			$event[0]['room']	= 0;
			$event[0]['userto']	= $userid;
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** ERROR: '$goal' is not a valid number to add to your current word count.</b></span>");
		}
	} else if (eregi("^/waradd", $msg)) {
		$event[0]['parsed']	= 1;
		$warstats	= explode(" ", $msg);
		$cur		= $warstats[1];
		if (is_numeric($cur)) {
			$sql	= "UPDATE {$dbprefix}prefs SET user_word_current=user_word_current+$cur WHERE user_id={$userid}";
			$db->sql_query($sql);
			$p_row['user_word_current']	+= $cur;
			$msg	= "<span class=sysmsg><i>*** $username current word count: <b>{$p_row['user_word_current']}</b> (+{$cur})</i>";
			if ($p_row['user_word_current'] >= $p_row['user_word_goal']) {
				$msg .= "<br /><font color=red><i>*** $username has reached their goal!!</i></font>";
			}
			$msg	.= "</span>";
			$event[0]['msg']	= addslashes($msg);
		} else {
			$event[0]['room']	= 0;
			$event[0]['userto']	= $userid;
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** ERROR: '$cur' is not a valid number to add to your current word count.</b></span>");
		}
	} else if (eregi("^/warsub", $msg)) {
		$event[0]['parsed']	= 1;
		$warstats	= explode(" ", $msg);
		$cur		= $warstats[1];
		if (is_numeric($cur)) {
			$sql	= "UPDATE {$dbprefix}prefs SET user_word_current=user_word_current-$cur WHERE user_id={$userid}";
			$db->sql_query($sql);
			$p_row['user_word_current']	-= $cur;
			$msg	= "<span class=sysmsg><i>*** $username current word count: <b>{$p_row['user_word_current']}</b> (-{$cur})</i>";
			if ($p_row['user_word_current'] >= $p_row['user_word_goal']) {
				$msg .= "<br /><font color=red><i>*** $username has reached their goal!!</i></font>";
			}
			$msg	.= "</span>";
			$event[0]['msg']	= addslashes($msg);
		} else {
			$event[0]['room']	= 0;
			$event[0]['userto']	= $userid;
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** ERROR: '$cur' is not a valid number to subtract from your current word count.</b></span>");
		}
	} else if (eregi("^/warend", $msg)) {
		$event[0]['parsed']	= 1;
		$warstats	= explode(" ", $msg);
		$cur		= $warstats[1];
		if (is_numeric($cur)) {
			$p_row['user_word_current']	= $cur;
			$msg	= "<span class=sysmsg><i>*** $username ends their Word War (start: <b>{$p_row['user_word_start']}</b> current: <b>{$p_row['user_word_current']}</b> goal: <b>{$p_row['user_word_goal']}</b>)</i>";
			if ($p_row['user_word_current'] >= $p_row['user_word_goal']) {
				$msg .= "<br /><font color=red><i>*** $username has reached their goal!!</i></font>";
			}
		} else {
			$msg	= "<span class=sysmsg><i>*** $username ends their Word War (start: <b>{$p_row['user_word_start']}</b> current: <b>{$p_row['user_word_current']}</b> goal: <b>{$p_row['user_word_goal']}</b>)</i></span>";
		}
		$sql	= "UPDATE {$dbprefix}prefs SET user_word_current=0,user_word_start=0,user_word_goal=0 WHERE user_id={$userid}";
		$db->sql_query($sql);
		$event[0]['msg']	= addslashes($msg);
	} else if (eregi("^/warget", $msg)) {
		$event[0]['parsed']	= 1;
		$event[0]['msg']	= addslashes("<span class=sysmsg><i>*** $username Word War stats -- start: <b>{$p_row['user_word_start']}</b> current: <b>{$p_row['user_word_current']}</b> goal: <b>{$p_row['user_word_goal']}</b></i></span>");
	} else if (eregi("^/warstart", $msg)) {
		$event[0]['parsed']	= 1;
		$warstats	= explode(" ", $msg);
		if (!is_numeric($warstats[1])) {
			$event[0]['room']	= 0;
			$event[0]['userto']	= $userid;
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** ERROR: format is /warstart [start] [goal]</b></span>");
		} else if (!is_numeric($warstats[2])) {
			$event[0]['room']	= 0;
			$event[0]['userto']	= $userid;
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** ERROR: format is /warstart [start] [goal]</b></span>");
		} else {
			$sql	= "UPDATE {$dbprefix}prefs SET user_word_current={$warstats[1]},user_word_start={$warstats[1]},user_word_goal={$warstats[2]} WHERE user_id={$userid}";
			$db->sql_query($sql);
			$event[0]['msg']	= addslashes("<span class=sysmsg><i>*** $username start: <b>{$warstats[1]}</b> goal: <b>{$warstats[2]}</b></i></span>");
		}
	} else if (eregi("^/warstats", $msg)) {
		$event[0]['parsed']	= 1;
		$sql	= "SELECT *
				FROM
					{$dbprefix}prefs AS p,
					{$dbprefix}sessions AS s
				WHERE
					p.user_id=s.user_id AND
					s.room_id=$room AND
					user_word_goal!=0
				ORDER BY
					user_alias";
		$result	= $db->sql_query($sql);
		$msg	= "<table width=100% border=1><tr><td class=row1>User</td><td class=row1>Start</td><td class=row1>Current</td><td class=row1>Goal</td></tr>";
		while ($lrow = $db->sql_fetchrow($result)) {
			$msg	.= "<tr><td class=row1>{$lrow['user_alias']}</td>";
			$msg	.= "<td class=row1>{$lrow['user_word_start']}</td>";
			$msg	.= "<td class=row1>{$lrow['user_word_current']}</td>";
			$msg	.= "<td class=row1>{$lrow['user_word_goal']}</td>";
			$msg	.= "</tr>";
		}
		$msg	.= "</table>";

		$event[0]['msg']	= addslashes($msg);
		$event[0]['room']	= 0;
		$event[0]['userto']	= $userid;
	} else if (eregi("^/wartimer", $msg)) {
		$event[0]['parsed']	= 1;
		$wartime		= explode(" ", $msg, 3);
		if (!is_numeric($wartime[1])) {
			$event[0]['msg']	= addslashes("<span class=sysmsg><i>*** $username starts a 1 hour Word War timer... on your marks!</i></span>");
			$time			= 60;
		} else {
			$time			= $wartime[1];
			$event[0]['msg']	= addslashes("<span class=sysmsg><i>*** $username starts a $time minute Word War timer... on your marks!</i></span>");
		}

		$ttime	= $time + 2;
		$sql	= "INSERT INTO {$dbprefix}alarms (alarm_user_id, alarm_room_to, alarm_time, alarm_text)
				VALUES ($userid, $room, NOW()+INTERVAL 1 MINUTE, '<span class=sysmsg><i>*** Word War starting in 1 minute... get set!</i></span>')";
		$db->sql_query($sql);

		$sql	= "INSERT INTO {$dbprefix}alarms (alarm_user_id, alarm_room_to, alarm_time, alarm_text)
				VALUES ($userid, $room, NOW()+INTERVAL 2 MINUTE, '<span class=sysmsg><i>*** Word War starting... GO!</i></span>')";
		$db->sql_query($sql);

		if ($config['SOUNDS_ENABLED']) {
			$sql	= "INSERT INTO {$dbprefix}alarms (alarm_user_id, alarm_room_to, alarm_time, alarm_text, alarm_type)
					VALUES ($userid, $room, NOW()+INTERVAL 2 MINUTE, '{$config['CHAT_URL']}/sounds/gunshot.wav', 1)";
			$db->sql_query($sql);
		}

		if ($time > 15) {
			$wtime	= $ttime - 5;
			$sql	= "INSERT INTO {$dbprefix}alarms (alarm_user_id, alarm_room_to, alarm_time, alarm_text)
					VALUES ($userid, $room, NOW()+INTERVAL '$wtime' MINUTE, '<span class=sysmsg><i>*** 5 minute warning until the Word War ends!</i></span>')";
		} else {
			$wtime	= $ttime - 1;
			$sql	= "INSERT INTO {$dbprefix}alarms (alarm_user_id, alarm_room_to, alarm_time, alarm_text)
					VALUES ($userid, $room, NOW()+INTERVAL '$wtime' MINUTE, '<span class=sysmsg><i>*** 1 minute warning until the Word War ends!</i></span>')";
		}
		$db->sql_query($sql);
		if ($config['SOUNDS_ENABLED']) {
			$sql	= "INSERT INTO {$dbprefix}alarms (alarm_user_id, alarm_room_to, alarm_time, alarm_text, alarm_type)
					VALUES ($userid, $room, NOW()+INTERVAL '$wtime' MINUTE, '{$config['CHAT_URL']}/sounds/timer.wav', 1)";
			$db->sql_query($sql);
		}

		if ($wartime[2] != '') {
			$sql	= "INSERT INTO {$dbprefix}alarms (alarm_user_id, alarm_room_to, alarm_time, alarm_text)
					VALUES ($userid, $room, NOW()+INTERVAL '$ttime' MINUTE, '<span class=sysmsg><i>*** Word War started by " . addslashes($username) . " has ended -- " . $wartime[2] . "!</i></span>')";
		} else {
			$sql	= "INSERT INTO {$dbprefix}alarms (alarm_user_id, alarm_room_to, alarm_time, alarm_text)
					VALUES ($userid, $room, NOW()+INTERVAL '$ttime' MINUTE, '<span class=sysmsg><i>*** Word War started by " . addslashes($username) . " has ended!</i></span>')";
		}
		$db->sql_query($sql);
		if ($config['SOUNDS_ENABLED']) {
			$sql	= "INSERT INTO {$dbprefix}alarms (alarm_user_id, alarm_room_to, alarm_time, alarm_text, alarm_type)
					VALUES ($userid, $room, NOW()+INTERVAL '$ttime' MINUTE, '{$config['CHAT_URL']}/sounds/whip.wav', 1)";
			$db->sql_query($sql);
		}

	} else if (eregi("^/warextend", $msg)) {
		$wartime		= explode(" ", $msg, 3);
		$event[0]['parsed']	= 1;
		$extend			= $wartime[1];
		if (!is_numeric($wartime[1])) {
			$event[0]['room']	= 0;
			$event[0]['userto']	= $userid;
			$event[0]['msg']	= addslashes("<span class=sysmsg><b>*** ERROR: format is /warextend [time] [mine|all]</b></span>");
		}

		if (strtolower($wartime[2]) != 'all' || !$isMod) {
			$sql			= "UPDATE {$dbprefix}alarms SET alarm_time=alarm_time+INTERVAL '$extend' MINUTE WHERE alarm_inactive=0 AND alarm_user_id=$userid";
			$event[0]['msg']	= addslashes("<span class=sysmsg><i>*** $username extends their Word War timer by $extend minute(s).</i></span>");
		} else {
			$sql			= "UPDATE {$dbprefix}alarms SET alarm_time=alarm_time+INTERVAL '$extend' MINUTE WHERE alarm_inactive=0";
			$event[0]['msg']	= addslashes("<span class=sysmsg><i>*** $username extends all active Word War timers by $extend minute(s).</i></span>");
		}
		$db->sql_query($sql);
	}
}
?>
